%% -*- erlang -*-
%% complex lager example
%% @doc where do you want the console.log output:
%% off : nowhere
%% file: the file specified by log.console.file
%% console : standard out
%% both : log.console.file and standard out.
{mapping, "log.service", "lager.service",
 [{default, "{{service}}"},
  {datatype, string}]}.

{mapping, "log.console", "lager.handlers",
 [{default, file},
  {datatype, {enum, [off, file, console, both]}}]}.

%% @doc the log level of the console log
{mapping, "log.console.level", "lager.handlers",
 [{default, info},
  {datatype, {enum, [debug, info, warning, error]}}]}.

%% @doc location of the console log
{mapping, "log.console.file", "lager.handlers",
 [{datatype, file},
  {default, "{{log_path}}/console.log"}]}.

%% @doc location of the error log
{mapping, "log.error.file", "lager.handlers",
 [{datatype, file},
  {default, "{{log_path}}/error.log"}]}.

%% @doc location of the debug log
{mapping, "log.debug.file", "lager.handlers",
 [{datatype, [{atom, off}, file]},
  {default, off},
  {commented, "{{log_path}}/debug.log"}]}.

%% *gasp* notice the same @mapping!
%% @doc turn on syslog
{mapping, "log.syslog", "lager.handlers",
 [{default, off},
  {datatype, {enum, [on, off]}}]}.

{translation,
 "lager.handlers",
 fun(Conf) ->
         Service = cuttlefish:conf_get("log.service", Conf),
         SyslogHandler =
             case cuttlefish:conf_get("log.syslog", Conf) of
                 on ->  [{lager_syslog_backend, [Service, daemon, info]}];
                 _ -> []
             end,
         ErrorHandler =
             case cuttlefish:conf_get("log.error.file", Conf) of
                 undefined -> [];
                 ErrorFilename -> [{lager_file_backend, [{file, ErrorFilename},
                                                         {level, error},
                                                         {size, 10485760},
                                                         {date, "$D0"},
                                                         {count, 5}]}]
             end,

         ConsoleLogLevel = cuttlefish:conf_get("log.console.level", Conf),
         ConsoleLogFile = cuttlefish:conf_get("log.console.file", Conf),

         ConsoleHandler = {lager_console_handler, ConsoleLogLevel},
         ConsoleFileHandler = {lager_file_backend, [{file, ConsoleLogFile},
                                                    {level, ConsoleLogLevel},
                                                    {size, 10485760},
                                                    {date, "$D0"},
                                                    {count, 5}]},

         ConsoleHandlers = case cuttlefish:conf_get("log.console", Conf) of
                               off -> [];
                               file -> [ConsoleFileHandler];
                               console -> [ConsoleHandler];
                               both -> [ConsoleHandler, ConsoleFileHandler];
                               _ -> []
                           end,
         DebugHandler =
             case cuttlefish:conf_get("log.debug.file", Conf) of
                 undefined -> [];
                 off -> [];
                 "off" -> [];
                 DebugFilename -> [{lager_file_backend, [{file, DebugFilename},
                                                         {level, debug},
                                                         {size, 10485760},
                                                         {date, "$D0"},
                                                         {count, 5}]}]
             end,

         SyslogHandler ++ ConsoleHandlers ++ ErrorHandler ++ DebugHandler
 end
}.

%% Lager Config

%% @doc Whether to write a crash log, and where.
%% Commented/omitted/undefined means no crash logger.
{mapping, "log.crash.file", "lager.crash_log",
 [{default, "{{log_path}}/crash.log"}]}.

%% @doc Maximum size in bytes of events in the crash log - defaults to 65536
%% @datatype integer
%% @mapping
{mapping, "log.crash.msg_size", "lager.crash_log_msg_size",
 [{default, "64KB"},
  {datatype, bytesize}]}.

%% @doc Maximum size of the crash log in bytes, before its rotated, set
%% to 0 to disable rotation - default is 0
{mapping, "log.crash.size", "lager.crash_log_size",
 [{default, "10MB"},
  {datatype, bytesize}]}.

%% @doc What time to rotate the crash log - default is no time
%% rotation. See the lager README for a description of this format:
%% https://github.com/basho/lager/blob/master/README.org
{mapping, "log.crash.date", "lager.crash_log_date",
 [{default, "$D0"}]}.

%% @doc Number of rotated crash logs to keep, 0 means keep only the
%% current one - default is 0
{mapping, "log.crash.count", "lager.crash_log_count",
 [{default, 5},
  {datatype, integer}]}.

%% @doc Whether to redirect error_logger messages into lager - defaults to true
{mapping, "log.error.redirect", "lager.error_logger_redirect",
 [{default, on},
  {datatype, {enum, [on, off]}}]}.

{translation,
 "lager.error_logger_redirect",
 fun(Conf) ->
         Setting = cuttlefish:conf_get("log.error.redirect", Conf),
         case Setting of
             on -> true;
             off -> false;
             _Default -> true
         end
 end}.

%% @doc maximum number of error_logger messages to handle in a second
%% lager 2.0.0 shipped with a limit of 50, which is a little low for riak's startup
{mapping, "log.error.messages_per_second", "lager.error_logger_hwm",
 [{default, 100},
  {datatype, integer}]}.

%% SASL
%% We should never care about this
{mapping, "sasl", "sasl.sasl_error_logger",
 [{default, off},
  {datatype, {enum, [on, off]}},
  {level, advanced}]}.

{translation,
 "sasl.sasl_error_logger",
 fun(Conf) ->
         case cuttlefish:conf_get("sasl", Conf) of %%how to pull default?
             on -> true;
             _ -> false
         end
 end
}.
